﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace custom
{
    internal class StartAndStop
    {
        private TabControl tabControl;
        private TabPage tabPage;
        private TextBox textBox;
        private BoolWrapper isCommandExecuted = new BoolWrapper { Value = false };
        private string[] value;
        public StartAndStop(TabControl tabControl, TabPage tabPage,TextBox textBox, string[] value)
        {
            this.tabControl = tabControl;
            this.tabPage = tabPage;
            this.textBox = textBox;
            this.value = value;
        }
        public class BoolWrapper
        {
            public bool Value { get; set; }
        }
        public async void Button_Click_Start(object sender, EventArgs e)
        {
            // 切换到tabControl1控件
            tabControl.Select();
            // 切换到textBox1控件
            tabControl.SelectedTab = tabPage;
            textBox.Focus();
            //判断是否第一次执行
            if (!isCommandExecuted.Value)
            {
                //清除之前的服务停止的消息
                textBox.Clear();
                // 将标志变量设置为true
                isCommandExecuted.Value = true;
                // 设置进程属性
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.WorkingDirectory = @value[1];
                process.StartInfo.Arguments = value[2];
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.CreateNoWindow = true;
                // 注册事件，获取cmd输出
                process.OutputDataReceived += new DataReceivedEventHandler((s, args) =>
                {
                    if (!String.IsNullOrEmpty(args.Data))
                    {
                        // 在文本框中输出cmd输出
                        textBox.Invoke(new Action(() =>
                        {
                            textBox.AppendText(args.Data + Environment.NewLine);
                        }));
                    }
                });

                // 启动进程
                process.Start();
                process.BeginOutputReadLine();
                
                // 等待进程结束
                await Task.Run(() => process.WaitForExit());
            }
        }
        public void Button_Click_Stop(object sender, EventArgs e)
        {
            textBox.Clear();
            // 切换到tabControl1控件
            tabControl.Select();
            // 切换到textBox1控件
            tabControl.SelectedTab = tabPage;
            textBox.Focus();
            isCommandExecuted.Value = false;
            // 创建进程对象
            Process process1 = new Process();
            // 设置进程属性
            process1.StartInfo.FileName = "cmd.exe";
            string kill = value[3];
            process1.StartInfo.Arguments = kill;
            process1.StartInfo.UseShellExecute = false;
            process1.StartInfo.RedirectStandardOutput = true;
            process1.StartInfo.CreateNoWindow = true;
            // 启动进程
            process1.Start();
            // 等待进程结束
            process1.WaitForExit();
            textBox.AppendText(value[0] + "服务已停止");
            
        }
    }
}
